<template>
  <el-dialog :modal-append-to-body="false" @close="fpgcjlCancel('fpgcjlRuleForm')" :close-on-click-modal="false" :title="detailData.status == 1030 ? '分配勘场工程经理' : '分配施工工程经理'
    " :visible.sync="fpgcjVisible" width="500px">
    <el-form :model="fpgcjlRuleForm" :rules="fpgcjlRules" ref="fpgcjlRuleForm" label-position="top">
      <el-form-item label="工程经理" prop="engineering_manager_id">
        <el-select class="input-box" v-model="fpgcjlRuleForm.engineering_manager_id" placeholder="请选择" filterable
          clearable @change="engineering_managerFunc">
          <el-option v-for="item in engineeringSupervisionList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工程监理" prop="engineering_supervision_id" v-if="detailData.status != 1030">
        <el-select class="input-box" v-model="fpgcjlRuleForm.engineering_supervision_id" placeholder="请选择" filterable
          clearable :disabled="!fpgcjlRuleForm.engineering_manager_id">
          <el-option v-for="item in engineeringSupervisionIdList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input :rows="4" class="input-box" v-model="fpgcjlRuleForm.remark" type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="fpgcjlCancel('fpgcjlRuleForm')">关闭</el-button>
      <el-button type="primary" @click="fpgcjlSubmit('fpgcjlRuleForm')" :disabled="diaLoading" :loading="diaLoading">确
        定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import projectApi from "@/api/projectApi";
import adminApi from "@/api/adminApi";

export default {
  name: "",
  props: {
    addDialogVisible: {
      type: Boolean,
      default: false,
      required: false,
    },
    project_id: {
      required: false,
    },
    detailData: {
      required: false,
    },
  },
  data() {
    return {
      engineeringSupervisionList: [],
      engineeringSupervisionIdList:[],
      fpgcjlRules: {
        engineering_manager_id: [
          { required: true, message: "请选择工程经理", trigger: "change" },
        ],
        engineering_supervision_id:[
        { required: true, message: "请选择工程监理", trigger: "change" },
        ]
      },
      fpgcjVisible: false,
      fpgcjlRuleForm: {
        engineering_manager_id: "",
        engineering_supervision_id:'',
        remark: "",
      },
      diaLoading: false,
    };
  },
  watch: {
    addDialogVisible(val1, val2) {
      if (this.addDialogVisible) {
        this.getEngineering_directorList(7);
        this.getEngineering_directorList(8);
      }
      this.$nextTick(() => {
        this.fpgcjVisible = this.addDialogVisible;
      });
    },
  },
  computed: {},
  mounted() { },
  methods: {
    // 工程经理选择
    engineering_managerFunc(e){
      if(!e){
        this.fpgcjlRuleForm.engineering_supervision_id = '';
        return;
      }
      let obj = this.engineeringSupervisionList.find(item=>{
        return item.id == e;
      })
      this.fpgcjlRuleForm.engineering_supervision_id = obj.engineering_supervision_id || '';
    },
    getEngineering_directorList(type) {
      var obj = {
        page: 1,
        num: 9999,
        keyword: "",
        province_id: "",
        city_id: "",
        county_id: "",
        identity: type == 7 ? 2 : 1,
        position_id_arr: [type],
      };
      adminApi
        .getAdminList(obj)
        .then((res) => {
          console.log(res, type == 8?"工程监理8":"工程经理7");
          if (res.code == 200) {
            if(type == 7){
              this.engineeringSupervisionList = res.data.list;
            }else{
              this.engineeringSupervisionIdList = res.data.list;
            }
          }
        })
        .catch((err) => { });
    },
    // 分配工程经理提交
    fpgcjlSubmit(formName) {
      let fpgcjlRuleForm = this.fpgcjlRuleForm;
      console.log(fpgcjlRuleForm, "fpgcjlRuleForm", this.project_id);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.diaLoading = true;
          projectApi
            .assignEngineeringManager({
              project_id: this.project_id,
              engineering_manager_id: fpgcjlRuleForm.engineering_manager_id,
              remark: fpgcjlRuleForm.remark,
              type: this.detailData.status == 1030 ? 1 : 2,//1勘场2施工
              engineering_supervision_id:this.detailData.status == 1030 ? '' : fpgcjlRuleForm.engineering_supervision_id
            })
            .then((res) => {
              console.log(res, "分配工程经理");
              this.diaLoading = false;
              if (res.code == 200) {
                this.$emit("onSuccess", true);
              }
              this.$message({
                type: res.code == 200 ? "success" : "warning",
                message: res.code == 200 ? "处理成功" : res.message,
              });
            })
            .catch((err) => {
              this.diaLoading = false;
              this.$message({
                type: "warning",
                message: err.message,
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    fpgcjlCancel(formName) {
      this.fpgcjlRuleForm = {
        engineering_manager_id: "",
        remark: "",
      };
      this.$nextTick(() => {
        this.$refs[formName].clearValidate();
        this.$emit("onClose", true);
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>